﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BasketStats.Core.Team;
using BasketStats.Core.Stats;

namespace BasketStats
{
    public partial class Court : Form
    {
        private Point Center = new Point(301, 86);
        private const int AreaRadius = 249;
        private bool m_missed = false;

        public Player Player { get; set; }

        public Court()
        {
            InitializeComponent();
        }

        private bool IsThreePoint(Point p)
        {
            var distFromCenter = Math.Sqrt((p.X - Center.X) * (p.X - Center.X) + (p.Y - Center.Y) * (p.Y - Center.Y));

            return (p.Y < 86 && (p.X < 50 || p.X > 551)) || (p.Y >= 86 && (distFromCenter > AreaRadius));
        }

        private void SetCursorIcon()
        {
            if (IsThreePoint(picCourt.PointToClient(new Point(Cursor.Position.X, Cursor.Position.Y))))
            {
                var img = (m_missed) ? Properties.Resources._3Missed.GetHicon() : Properties.Resources._3Scored.GetHicon();
                Cursor = new Cursor(img);
            }
            else
            {
                var img = (m_missed) ? Properties.Resources._2Missed.GetHicon() : Properties.Resources._2Scored.GetHicon();
                Cursor = new Cursor(img);
            }

        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            SetCursorIcon();
        }

        private void Court_KeyUp(object sender, KeyEventArgs e)
        {
            if (!e.Control)
            {
                m_missed = false;
                SetCursorIcon();
            }
        }

        private void Court_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Control)
            {
                m_missed = true;
                SetCursorIcon();
            }
        }

        private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
        {
            if (Player != null)
            {
                var p = picCourt.PointToClient(new Point(Cursor.Position.X, Cursor.Position.Y));

                Player.CurrentStatistics.Shots.Add(new Shot
                                        {
                                            x = (double)p.X / picCourt.Size.Width,
                                            y = (double)p.Y / picCourt.Size.Height,
                                            Scored = !m_missed,
                                            Type = (IsThreePoint(p)) ? ShotType.TPT : ShotType.FG
                                        });
                Close();
            }
        }


    }
}
